package com.jiang.books.domain;

import org.hibernate.annotations.Type;
import org.hibernate.envers.Audited;
import org.hibernate.envers.RelationTargetAuditMode;
import org.joda.time.DateTime;
import org.springframework.data.domain.Auditable;

import javax.persistence.*;

import java.io.Serializable;
import java.util.Arrays;

import static javax.persistence.GenerationType.SEQUENCE;

@Entity
@Table(name = "jiang_profile")
@NamedQueries({
        @NamedQuery(name="Profile.findAll",
                query="select c from Profile c"),
        @NamedQuery(name="Profile.findById",
                query="select distinct c from Profile c where c.id = :id")
})
@SqlResultSetMapping(
        name="ProfileResult",
        entities=@EntityResult(entityClass=Profile.class))
@Audited
public class Profile implements Serializable, Auditable<String, Long> {

    private static final long serialVersionUID = 1L;

    private Long id;
    private User user;
    private byte[] userPhoto;
    private String aboutMe;

    private Country country;

    // Audit fields
    private String createdBy;
    private DateTime createdDate;
    private String lastModifiedBy;
    private DateTime lastModifiedDate;

    @Id
    @GeneratedValue(strategy = SEQUENCE, generator="JIANG_SEQ")
    @SequenceGenerator(name="JIANG_SEQ", sequenceName="jiang_id_sequence")
    @Column(name = "ID", unique = true, nullable = false)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @ManyToOne
    @JoinColumn(name = "user_id")
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Lob
    @Column(name = "user_photo")
    public byte[] getUserPhoto() {
        return userPhoto;
    }

    public void setUserPhoto(byte[] userPhoto) {
        this.userPhoto = userPhoto;
    }

    @Column(name = "about_me")
    public String getAboutMe() {
        return aboutMe;
    }

    public void setAboutMe(String aboutMe) {
        this.aboutMe = aboutMe;
    }

    @ManyToOne
    @JoinColumn(name = "country_id")
    @Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
    public Country getCountry() {
        return country;
    }

    public void setCountry(Country country) {
        this.country = country;
    }


    // Audit fields
    @Column(name="CREATED_BY")
    public String getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    @Column(name="CREATED_DATE")
    @Type(type="org.joda.time.contrib.hibernate.PersistentDateTime")
    public DateTime getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(DateTime createdDate) {
        this.createdDate = createdDate;
    }

    @Column(name="LAST_MODIFIED_BY")
    public String getLastModifiedBy() {
        return lastModifiedBy;
    }

    public void setLastModifiedBy(String lastModifiedBy) {
        this.lastModifiedBy = lastModifiedBy;
    }

    @Column(name="LAST_MODIFIED_DATE")
    @Type(type="org.joda.time.contrib.hibernate.PersistentDateTime")
    public DateTime getLastModifiedDate() {
        return lastModifiedDate;
    }

    public void setLastModifiedDate(DateTime lastModifiedDate) {
        this.lastModifiedDate = lastModifiedDate;
    }

    @Transient
    public boolean isNew() {
        if (id == null) {
            return true;
        } else {
            return false;
        }
    }


    @Override
    public String toString() {
            return "Profile{" +
                    "id=" + id +
                    ", user=" + user +
                    //", userPhoto=" + Arrays.toString(userPhoto) +
                    ",\n userPhoto=" + Arrays.toString(userPhoto) +
                    ", aboutMe='" + aboutMe + '\'' +
                    ", createdBy='" + createdBy + '\'' +
                    ", createdDate=" + createdDate +
                    ", lastModifiedBy='" + lastModifiedBy + '\'' +
                    ", lastModifiedDate=" + lastModifiedDate +
                    '}';
    }
}